
function PageContextInputTree(settings){
	var _this = this; //把this保存下来，以后用_this代替this，这样就不会被this弄晕了
	this.show =  false;	
	var defaults = {
			formId:"#searchForm"
			,target:null
			,inputName:".treeSel"
			,inputValue:".treeValue"
			,menuContent:".menuContent"
		    ,dialog:false
		    ,onInputTreeBodyDown:null
	};
	this.setting = $.extend( true,  {}, defaults, settings || {}); 
	this.obj = null;
	if(this.setting.target!=null){
		this.setting.target.on("click",function(){
			_this.showMenu(this);
		});
		
		this.setting.target.next("a").on("click",function(){
			_this.clear(this);
		});
	}
	this.clear = function (me){
		var cityObjName = $(me).closest("form").find(_this.setting.inputName);
		cityObjName.val("");
		var cityObj = $(me).closest("form").find(_this.setting.inputValue);
		cityObj.val("");
		return false;
	}
	 this.hideMenu = function() {
		 var me = _this.obj;
		 $(me).closest("form").find(_this.setting.menuContent).fadeOut("fast");
		 $(me).closest("body").unbind("mousedown", _this.setting.onInputTreeBodyDown);
		_this.show =  false;
	}
	 this.showMenu = function (me) {
		 _this.obj= me;
			if(_this.show){
				_this.hideMenu(me);
			}else{
				var left = 0,top=0,w=0;
				var cityObj = $(me);
//				var cityOffset = cityObj.offset();
//				var left = cityOffset.left,top = cityOffset.top;
//				console.log(left)
//				left+=8
////				$(me).closest("form").find(_this.setting.menuContent).css({left:cityOffset.left + "px", top:cityOffset.top + cityObj.outerHeight() + "px"}).slideDown("fast");
////				left -= $(me).prev("input").width()+15;
//				top += cityObj.outerHeight()-5;
				w = cityObj.outerWidth();
				top = cityObj.outerHeight();
//				if(_this.setting.dialog){
//					left = 0;
//					top = top-100;
//				}
				var content = $(me).closest("form").find(_this.setting.menuContent);
				content.css({left: left+ "px",top:top+"px",width:w+"px"}).slideDown("fast");
				content.find("ul").css({width:w+"px"})
				$(me).closest("body").bind("mousedown", _this.setting.onInputTreeBodyDown);
				_this.show = true;
			}
		}
	this.onClickSearch = function (e, treeId, treeNode) {
		var me = _this.obj;
	 	var nodes = _this.getSelectedNodes();
	 	var n = "";
	 	nodes.sort(function compare(a,b){return a.id-b.id;});
	 	for (var i=0, l=nodes.length; i<l; i++) {
	 		n += nodes[i].name + ",";
	 	}
	 	if (n.length > 0 ) n = n.substring(0, n.length-1);

	 	var v = "";
	 	for (var i=0, l=nodes.length; i<l; i++) {
	 		v += nodes[i].id + ",";
	 	}
	 	if (v.length > 0 ) v = v.substring(0, v.length-1);
	 	var cityObjName =$(me).closest("form").find(_this.setting.inputName);
	 	cityObjName.val(n);
	 	var cityObj = $(me).closest("form").find(_this.setting.inputValue);
	 	cityObj.val(v);
	 }
	this.initTree = function(target,treeData,setting){
		var defaults = {
				view: {
					dblClickExpand: false
				},
				data: {
					simpleData: {
						enable: true
					}
				},
				callback: {
					//beforeClick: beforeClickSearch,
					onClick: onInputTreeClickSearch
				}
		};
		_this.treeSetting = $.extend( true,  {}, defaults, setting || {}); 
		_this.treeObj = $.fn.zTree.init(target, _this.treeSetting, treeData);
	};
	
	this.expandNode = function(id){
		_this.treeObj.expandNode(_this.getNode(id), true, false, true);
	}
	this.notExpandNode = function(id){
		_this.treeObj.expandNode(_this.getNode(id), false, true, true);
	}
	
	this.getChildNodes = function(id){
		var node = _this.getNode(id);
		var nodes = _this.treeObj.getNodesByFilter(function(node){
			return true;
		},false,node);
		return nodes;
	}
	
	/**
	 * 获取checked的节点
	 */
	this.getCheckedNodes = function(){
		var nodes = _this.treeObj.getCheckedNodes(true);
		var array = [];
		if(nodes && nodes.length>0){
			 for(n in nodes){
				 array.push(nodes[n]);
			 }
		}
		return array;
	};
	this.getCheckedNodeIds= function(){
		var nodes = _this.treeObj.getCheckedNodes(true);
		var array = [];
		if(nodes && nodes.length>0){
			 for(n in nodes){
				 array.push(nodes[n].id);
			 }
		}
		return array;
	};
	
	
	/**
	 * 获取选中的节点
	 */
	this.getSelectedNodeIds = function(){
		var nodes = _this.treeObj.getSelectedNodes();
		var array = [];
		if(nodes && nodes.length>0){
			 for(n in nodes){
				 array.push(nodes[n].id);
			 }
		}
		return array;
	};

	/**
	 * 获取选中的节点
	 */
	this.getSelectedNodes = function(){
		var nodes = _this.treeObj.getSelectedNodes();
		var array = [];
		if(nodes && nodes.length>0){
			 for(n in nodes){
				 array.push(nodes[n]);
			 }
		}
		return array;
	};
	/**
	 * 选中
	 */
	this.selectNode = function(id){
		var node = _this.getNode(id);
		this.treeObj.selectNode(node,false,false);
		this.expandNode(node.pId);
	};
	/**
	 * 取消选中
	 */
	this.cancelSelectedNode = function(id){
		var node = _this.getNode(id);
		this.treeObj.cancelSelectedNode(node,false,false);
		this.notExpandNode(node.pId);
	};
	
	/**
	 * 获取节点
	 */
	this.getNode = function(id){
		return this.treeObj.getNodesByParam("id", id, null)[0];
	};
}
